import nyNetwork from "@/api/nyBaseNetwork";
import DZModelPage from "@/model/DZModelPage";
import uploadFile from "@/utils/DZUploadFile";
import nyMessageToast from "@/utils/NYMessageToast";
import DZModelCompany from "../model/DZModelCompany";
import DZModelCover from "../model/DZModelCover";

class DZServiceCompany {

    m_page = new DZModelPage(10);

    str_key = "";

    list_company: Array<DZModelCompany> = []

    m_company = new DZModelCompany();

    list_cover: Array<DZModelCover> = [];

    net_list_company() {
        var arr_data = [];
        var param = { ...this.m_page, name: this.str_key }
        nyNetwork.net("公司列表", param, data => {
            this.m_page.total = data.total;
            var dataList = data.list;
            for (const item of dataList) {
                var company = new DZModelCompany();
                company.id = item.iId ?? "";
                company.name = item.sName ?? "";
                company.assess = item.sAssessList ?? "";
                company.show = item.iShowReportType ?? 1;
                company.assess_num = item.iAssessNum ?? 0;
                if (company.assess) {
                    var array = company.assess.split(",");
                    for (const id of array) {
                        company.assess_list.push(parseInt(id))
                    }
                }
                company.status = item.iStatus ?? 1;
                arr_data.push(company);
            }
            this.list_company = arr_data;
        })
    }

    net_frozen_company(id, success: () => void) {
        nyNetwork.net("冻结解冻公司", { id: id }, data => {
            success();
        })
    }

    net_del_company(id, success: () => void) {
        nyNetwork.net("删除公司", { id: id }, data => {
            success();
        })
    }

    net_add_company(success: () => void) {
        nyNetwork.net("添加公司", { name: this.m_company.name, assess: this.m_company.assess_list.join(","), showType: this.m_company.show, num: this.m_company.assess_num }, data => {
            success();
        })
    }

    net_edit_company(success: () => void) {
        nyNetwork.net("编辑公司", {
            id: this.m_company.id, name: this.m_company.name, assess: this.m_company.assess_list.join(","),
            showType: this.m_company.show, num: this.m_company.assess_num
        }, data => {
            success();
        })
    }

    net_list_cover(companyId) {
        var arr_data = [];
        nyNetwork.net("封面列表", { companyId: companyId }, data => {
            for (const item of data) {
                var cover = new DZModelCover();
                cover.assess_id = item.iAssessId ?? '';
                cover.assess_name = item.sAssessName ?? '';
                cover.company_id = item.iCompanyId ?? '';
                cover.cover_id = item.iReportCover ?? '';
                if (item.iReportCover) {
                    cover.cover_url = nyNetwork.image(item.iReportCover);
                    cover.cover_url_list.push(cover.cover_url)
                }
                arr_data.push(cover);
            }
            this.list_cover = arr_data;
        })
    }



    net_add_cover(assessId, companyId, file, success: () => void) {
        uploadFile.uploadImage(file,
            (res) => {
            }, (res) => {
                var imageId = res;
                nyNetwork.net("添加封面", { assessId: assessId, companyId: companyId, coverId: imageId }, data => {
                    success();
                })
            }, (res) => {
                nyMessageToast.error("图片上传失败")
            });
    }



}
export default DZServiceCompany;